<?php

namespace admin\components;

use app\components\BaseActiveRecord;
use common\models\Admin;
use common\utils\EncryptUtils;
use Yii;
use yii\base\UnknownPropertyException;
use yii\web\IdentityInterface;

class AdminUserIdentity implements IdentityInterface {

    public $id;
    public $model;

    /**
     * Finds an identity by the given ID.
     * @param string|integer $id the ID to be looked for
     * @return IdentityInterface the identity object that matches the given ID.
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
     */
    public static function findIdentity($id) {
        $model = Admin::findOne($id);
        if (!$model) {
            return FALSE;
        }
        $identity = new static();
        $identity->model = $model;
        return $identity;
    }

    /**
     * Finds an identity by the given token.
     * @param mixed $token the token to be looked for
     * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
     * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
     * @return IdentityInterface the identity object that matches the given token.
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
     */
    public static function findIdentityByAccessToken($token, $type = NULL) {
        // TODO: Implement findIdentityByAccessToken() method.
    }

    /**
     * Returns an ID that can uniquely identify a user identity.
     * @return string|integer an ID that uniquely identifies a user identity.
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Returns a key that can be used to check the validity of a given identity ID.
     *
     * The key should be unique for each individual user, and should be persistent
     * so that it can be used to check the validity of the user identity.
     *
     * The space of such keys should be big enough to defeat potential identity attacks.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @return string a key that is used to check the validity of a given identity ID.
     * @see validateAuthKey()
     */
    public function getAuthKey() {
        // TODO: Implement getAuthKey() method.
    }

    /**
     * Validates the given auth key.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @param string $authKey the given auth key
     * @return boolean whether the given auth key is valid.
     * @see getAuthKey()
     */
    public function validateAuthKey($authKey) {
    }

    public function authenticate(BaseActiveRecord $model) {
        $user = $model::findOne(['username' => $model['username']]);
        if (!$user) {
            return $model::retErr(Yii::t('admin', 'login.username.error'));
        }

        $_pass = EncryptUtils::md516($model['password']);
        if ($_pass != $user['password']) {
            return $model::retErr(Yii::t('admin', 'login.password.error'));
        }

        if (intval($user['status']) !== Admin::STATUS_ACTIVE) {
            return $model::retErr(Yii::t('admin', 'login.status.error'));
        }

        $this->id = $user->id;
        Yii::$app->user->login($this, $model['rememberMe'] ? 3600 * 24 * 3 : 0);

        return $model::retOK(['redirect' => Yii::$app->user->getReturnUrl(['/admin'])]);
    }

    public function __get($name) {
        if (!empty($this->model[$name])) {
            return $this->model[$name];
        }
        throw new UnknownPropertyException('Getting unknown property: ' . get_class($this->model) . '::' . $name);
    }
}